package com.ylb.rent.service.impl;

import com.xiaoleilu.hutool.util.StrUtil;
import com.ylb.rent.dao.UserDao;
import com.ylb.rent.entity.User;
import com.ylb.rent.exception.UserNotExistOrPasswordErrorException;
import com.ylb.rent.exception.ValidateCodeError;
import com.ylb.rent.service.IUserService;
import com.ylb.rent.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * @AUTHOR:游李滨
 * @DATE:2023/6/28 11:10
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserDao userDao;

    @Autowired
    TokenUtil tokenUtil;

    private void validateCode(String code)throws ValidateCodeError{
        if(StrUtil.hasEmpty(code) || StrUtil.hasBlank(code )){
            throw new ValidateCodeError();
        }
    }

    @Override
    public String login(String username, String password, String code) throws UserNotExistOrPasswordErrorException,ValidateCodeError{

        validateCode(code);

        User user = userDao.selectByUserName(username);
        if(user == null || !user.getPassword().equals(password)){
            throw  new UserNotExistOrPasswordErrorException();
        }

        return tokenUtil.generateToken(user.getId());
    }
}
